/**
 *  This file is part of TaoBaoItemImporter.
 *
 *   TaoBaoItemImporter is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   TaoBaoItemImporter is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with TaoBaoItemImporter.  If not, see <http://www.gnu.org/licenses/>. 
 */

package com.googlecode.ulysses.taobao.data;

import com.taobao.api.*;
import com.taobao.api.request.*;
import com.taobao.api.response.*; 

/**
 * @author Hegen <lhegen@gmail.com>
 *
 */
public class CreateCPSLink {
	private String url = null;
	private String appkey = null;
	private String secret = null;
	private String fields = null;;
	private long pid = 0L;
	public TaobaokeItemsConvertResponse response = null;
	
	public CreateCPSLink(String url, String appkey, String secret, String fields, long pid){
		this.url =url;
		this.appkey = appkey;
		this.secret = secret;
		this.fields = fields;
		this.pid = pid;
	}
	
	public String getCPSLink(String num_iids){
		//TaobaokeItemsConvertResponse response = null;
		String resultStr = null;
		try
		{
			TaobaoClient client=new DefaultTaobaoClient(this.url, this.appkey, this.secret);
			TaobaokeItemsConvertRequest req=new TaobaokeItemsConvertRequest();
			req.setFields(this.fields);
			req.setNumIids(num_iids);
			req.setPid(this.pid);
			response = client.execute(req);
		}
		catch(ApiException e){
			System.out.println(e.getMessage());
		}
		
		try{
			resultStr = response.getTaobaokeItems().get(0).getClickUrl().toString();
		}
		catch(NullPointerException e){
			return null;
		}
		
		return resultStr;
	}
	
	public String getCommission(){
		String commission = null;
		try{
			commission = response.getTaobaokeItems().get(0).getCommission().toString();
		}catch(NullPointerException e){
			return "0.0";
		}
		return commission;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getAppkey() {
		return appkey;
	}

	public void setAppkey(String appkey) {
		this.appkey = appkey;
	}

	public String getSecret() {
		return secret;
	}

	public void setSecret(String secret) {
		this.secret = secret;
	}

	public String getFields() {
		return fields;
	}

	public void setFields(String fields) {
		this.fields = fields;
	}

	public long getPid() {
		return pid;
	}

	public void setPid(long pid) {
		this.pid = pid;
	}
}
